package elitech.vietnam.myfashion.extend;

import java.util.List;

import android.content.Intent;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.koushikdutta.urlimageviewhelper.UrlImageViewHelper;

import elitech.vietnam.myfashion.LoginActivity;
import elitech.vietnam.myfashion.MainActivity;
import elitech.vietnam.myfashion.R;
import elitech.vietnam.myfashion.config.Const;
import elitech.vietnam.myfashion.dialog.OkCancelWarnningDialog;
import elitech.vietnam.myfashion.dialog.WarningDialog;
import elitech.vietnam.myfashion.doinbackground.CallServiceTask;
import elitech.vietnam.myfashion.entites.Product;
import elitech.vietnam.myfashion.extend.GridViewArrayAdapter.ViewHolder;
import elitech.vietnam.myfashion.fragment.DetailFragment;
import elitech.vietnam.myfashion.utilities.Utilities;
import elitech.vietnam.myfashion.widgets.ScaleImageView;

public class GridViewFarvoriteArrayAdapter extends ArrayAdapter<Product> {
	private MainActivity mContext;
	
	public GridViewFarvoriteArrayAdapter(MainActivity context, int resource,
			int textViewResourceId, List<Product> objects) {
		super(context, resource, textViewResourceId, objects);
		this.mContext = context;
	}

	@Override
	public void notifyDataSetChanged() {
		super.notifyDataSetChanged();
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View row = convertView;
		final Product product = getItem(position);
		final ViewHolder holder;
		
		if (row == null) {
			
			LayoutInflater inflater = mContext.getLayoutInflater();
			row = inflater.inflate(R.layout.item_favorite, parent, false);
			holder = new ViewHolder();
			
			holder.image = (ScaleImageView) row
					.findViewById(R.id.iproduct_image);
			holder.txtLike = (TextView) row
					.findViewById(R.id.iproduct_txtLikes);
			holder.txtName = (TextView) row
					.findViewById(R.id.iproduct_txtName);
			holder.txtPrice = (TextView) row.findViewById(R.id.iproduct_txtPrice);
			holder.layPrice = (LinearLayout) row
					.findViewById(R.id.iproduct_layPrice);
			holder.tagSaleOff = (ImageView) row
					.findViewById(R.id.iproduct_tagImgSaleOff);
			holder.layLikes = (LinearLayout) row
					.findViewById(R.id.iproduct_layBtnLike);
			row.setTag(holder);	
		}else
			holder = (ViewHolder) row.getTag();
		
		UrlImageViewHelper.setUrlDrawable(holder.image, Const.SERVER_IMAGE_URL + product.Image, R.drawable.loading300);
		holder.txtLike.setText(product.Likes + "");
		holder.txtName.setText(product.Name + "");
		holder.txtPrice.setText(Utilities.numberFormat(product.PriceVN)+" VNĐ");
		String color;
		if(product.CategoryColor == null || product.CategoryColor == "" || product.CategoryColor == "#")
			color = "#808080";
		else
			color = product.CategoryColor;
		
		holder.layPrice.setBackgroundColor(Color.parseColor(color));
		
		if (product.SaleOff != 0) {
			holder.tagSaleOff.setVisibility(View.VISIBLE);
			int idTag = Utilities.getSaleOffTagId(product.SaleOff);
			if (idTag != 0)
				holder.tagSaleOff.setImageResource(idTag);
		}

		if (product.Liked())
			holder.layLikes.setBackgroundResource(R.drawable.liked);
		else
			holder.layLikes.setBackgroundResource(R.drawable.like);
		
		holder.image.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				if (mContext.mDetailFragment == null)
					mContext.mDetailFragment = new DetailFragment();
				if (!mContext.mDetailFragment.isAdded()) {
					mContext.mDetailFragment.setProduct(product);
					mContext.replaceFragment(mContext.mDetailFragment, true);
				}
			}
		});
		
		holder.layLikes.setOnClickListener(new View.OnClickListener() {
			boolean isProcessing = false;
			@Override
			public void onClick(View v) {
				if(!isProcessing){
					if(mContext.mUser == null){
						new OkCancelWarnningDialog(mContext, mContext.getString(R.string.requestlogin)){
							public void onOk() {
								Intent i = new Intent(mContext, LoginActivity.class);
								mContext.startActivityForResult(i, MainActivity.LOGINACTIVITY);
							};
						}.show();
					}else{
						if(product.Liked()){
							new OkCancelWarnningDialog(mContext, mContext.getString(R.string.confirmunlikeproduct)){
								public void onOk() {
									new CallServiceTask(){
										private int rs = -2;
										protected void onPreExecute() {
											isProcessing = true;
										};
										public void work() {
											try {
												rs = service.newLike(mContext.mUser.Id, product.Id, product.Liked(), 1);
											} catch (Exception e) {
												e.printStackTrace();
											}
										};
										protected void onPostExecute(Void result) {
											
											if (rs == -2) {
												new WarningDialog(
														mContext,
														mContext.getString(R.string.connecterror))
														.show();
											} else if (rs == -1) {
												mContext.mListFavorite.remove(product);
												product.Likes = product.Likes-1;
												product.Liked = 0;
												notifyDataSetChanged();
											} else {
												mContext.mListFavorite.remove(product);
												product.Likes = rs;
												product.Liked = 0;
												notifyDataSetChanged();
											}
											isProcessing = false;
										};
									}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
								};
							}.show();
						}else{
							new CallServiceTask(){
								private int rs = -2;
								protected void onPreExecute() {
									isProcessing = true;
								};
								public void work() {
									try {
										rs = service.newLike(mContext.mUser.Id, product.Id, product.Liked(), 1);
									} catch (Exception e) {
										e.printStackTrace();
									}
								};
								protected void onPostExecute(Void result) {
									
									if (rs == -2) {
										new WarningDialog(
												mContext,
												mContext.getString(R.string.connecterror))
												.show();
									} else if (rs == -1) {
										product.Liked = 1;
										holder.layLikes.setBackgroundResource(R.drawable.liked);
									} else {
										holder.txtLike.setText(rs + "");
										product.Likes = rs;
										holder.layLikes.setBackgroundResource(R.drawable.liked);
										product.Liked = 1;
									}
									isProcessing = false;

								};
							}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
						}
					}
				}
			}
		});
		
		return row;
	}

}

